{"componentChunkName":"component---src-templates-blog-template-js","path":"/blog/azure-apim-custom-policy-for-rate-limiting","result":{"data":{"markdownRemark":{"html":"<div class=\"blog-post\">\n<h6><em><a href=\"#menu\">Back to the top</a></em></h6>\n<p><a name=\"azapimgt\" id=\"azapimgt\"></a></p>\n\n          <h2>\n            Apply custom policy-based rate limiting to Azure APIs using Azure API Management\n          </h2>\n        \n<p>2018-10-08</p>\n<p>While the Azure docs lead me think that if you are a Cloud Solution Provider (CSP) partner there is a way to limit API calls by CustomerId that is not available where you issue subscriptions to users.</p>\n<p>In our case each customer can have as many users as they wish. Each user has a subscription to an Azure Product (eg the Unlimited product).</p>\n<p>Just because we give access to the Unlimited product doesn't mean we want an unlimited flood of requests.</p>\n<p>What I needed to do was limit all the users for a company as a job lot to a given number of requests per minute.</p>\n<p><a href=\"https://docs.microsoft.com/en-us/azure/api-management/api-management-access-restriction-policies#SetUsageQuota\" target=\"_blank\" rel=\"noopener noreferrer\">Limiting by subscription (ie, API key)</a> is documented but we need a higher level grouping to rate-limit on. This does not seem to be available so I have done it this way:</p>\n<ul>\n<li>Created three users. All users by default (and this can't be changed) are put into Developers. I also created two groups, Customer1 and Customer2 and put two users into the first and one into the second.\n<a href=\"../images/api-policy-users.PNG\"><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 35.810810810810814%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA4UlEQVQoz4WRWW7DMAxEdf+LBkiaaF+9sHpMVLgfRQkQpkac4VA2N5uljyEpJbk/HjJmvXLbxq9z7/3fND5XiTFKKWUCTca2S8xDcumScpfautYIEud5aq76OI4fjDRpCrZatYHLOJ06F+aQpGI47x23m+ScpbUm+76rATA4dfKpSXP7CuKdVbtMsNZKCEGbJqDOEFiCfOljK++9CoPTg7h5Pe0bTFm/zjkVXAMIxBHA3VoREYbD4R5BXdnNKYgAkJAgI7jIEJbr5QRBeGBrZXVYanuv94nrA1/P18f/64cQ36F7I9Y4labsAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"api policy users\"\n        title=\"api policy users\"\n        src=\"/static/16011fa256b8e1ae70443c1bf2bde768/fcda8/api-policy-users.png\"\n        srcset=\"/static/16011fa256b8e1ae70443c1bf2bde768/12f09/api-policy-users.png 148w,\n/static/16011fa256b8e1ae70443c1bf2bde768/e4a3f/api-policy-users.png 295w,\n/static/16011fa256b8e1ae70443c1bf2bde768/fcda8/api-policy-users.png 590w,\n/static/16011fa256b8e1ae70443c1bf2bde768/efc66/api-policy-users.png 885w,\n/static/16011fa256b8e1ae70443c1bf2bde768/2cffa/api-policy-users.png 1092w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></a></li>\n<li>\n<p>Go API> All APIs >Inbound Processing > rate-limit-by-key</p>\n<p>This will let you edit the default policy on the APIs</p>\n</li>\n</ul>\n<p><a href=\"../images/api-policy-select-policy.PNG\"><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 31.08108108108108%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA2klEQVQY032Pi4rDIBBF/f9fzLbbbGmlKYmaURNfuXVkA6VkVzjgDHrmjiAXMCqNmSwMrVDKQM+ENeZGSv8TPxBEBK01QgjYtoIYI4bnE7P1IB9b/TcBOadKRqnklCCWZQHRjFiF1YhUH1prW48fb7VXSjnEOQ9j5rqVwjRNtXYQvJb1K1JBg2uezh9YdnT2fqqJWOK9BwfjLcXX4NE9HG4EXA1w06kJ949H8GFR13W49D3udwkpZUsq2Lryur+CUGU8+T3Je9L9Po4jTqczBr3g50HopcL5+4IXF2vT54DLPFgAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"api policy select policy\"\n        title=\"api policy select policy\"\n        src=\"/static/f9f1807f2a9747645d2ab8141f61e82b/fcda8/api-policy-select-policy.png\"\n        srcset=\"/static/f9f1807f2a9747645d2ab8141f61e82b/12f09/api-policy-select-policy.png 148w,\n/static/f9f1807f2a9747645d2ab8141f61e82b/e4a3f/api-policy-select-policy.png 295w,\n/static/f9f1807f2a9747645d2ab8141f61e82b/fcda8/api-policy-select-policy.png 590w,\n/static/f9f1807f2a9747645d2ab8141f61e82b/efc66/api-policy-select-policy.png 885w,\n/static/f9f1807f2a9747645d2ab8141f61e82b/6d2da/api-policy-select-policy.png 936w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></a></p>\n<ul>\n<li>\n<p>Edit the policy</p>\n<p>It's <a href=\"https://docs.microsoft.com/en-us/azure/api-management/api-management-policy-expressions\" target=\"_blank\" rel=\"noopener noreferrer\">C# 7 syntax</a> but there can be only one policy per product or API. So I've applied a limit on 12 requests in a 60 second period, with the count being incremented by the name of group where the group name starts with 'Customer'. This is to distinguish it from say the Developers group which treats all customers' users the same</p>\n<p>Doing this will keep track of the count of requests by customer no matter how many users they have, as long as each customer's users are correctly assigned to their group.</p>\n</li>\n</ul>\n<p><a href=\"../images/api-policy-policy.PNG\"><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 29.054054054054056%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA6klEQVQY05WQ23KDMAxE+f+vbBsC5Z4AvsiyvZUUJm95KMOZXWFbWNuEEJBzhup5HqBEKKUI+dLP1FoBfUUV7dMQkRWJGcQR9+kb09YLvxhFR/M95sfw9sNyx7bPcOHA4R6iu/lIHs3kKpQ9JDAT+qVFN98uftBOX0a3vOpx67A8B2v0PFdjF+/jKdNFNFwqOFd4uWlwJ9y2IlMExwCW8Tl4UIzIUqtSYpkmy3hFLlDsbJHpin1nNLCnIlCyA2t7Q5IsCyfLsWa+VDIVsmSnWSksa2xe9qjXhq9gpWFK8MeOYxpt0dL+J/rjP8G/00cgKQslAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"api policy policy\"\n        title=\"api policy policy\"\n        src=\"/static/66e860a77ef31f9b49e6b3909c14c746/fcda8/api-policy-policy.png\"\n        srcset=\"/static/66e860a77ef31f9b49e6b3909c14c746/12f09/api-policy-policy.png 148w,\n/static/66e860a77ef31f9b49e6b3909c14c746/e4a3f/api-policy-policy.png 295w,\n/static/66e860a77ef31f9b49e6b3909c14c746/fcda8/api-policy-policy.png 590w,\n/static/66e860a77ef31f9b49e6b3909c14c746/efc66/api-policy-policy.png 885w,\n/static/66e860a77ef31f9b49e6b3909c14c746/c83ae/api-policy-policy.png 1180w,\n/static/66e860a77ef31f9b49e6b3909c14c746/08037/api-policy-policy.png 1379w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></a></p>\n<ul>\n<li>\n<p>To see the policy in action (and how it processed) you'll need to use the <a href=\"https://docs.microsoft.com/en-us/azure/api-management/api-management-howto-api-inspector\" target=\"_blank\" rel=\"noopener noreferrer\">API inspector</a>.</p>\n<p>Go API> All APIs > Echo API > Test > GET</p>\n<p>This will let you test the default API</p>\n</li>\n</ul>\n<p><a href=\"../images/api-policy-setuptest.PNG\"><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 79.72972972972973%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAB0ElEQVQ4y41UXW/UMBDMz+8zP4iHSiB44CmFF1ALQr0eUi7JxXac+CPTHad79Ero1dIq67U9nt3ZuDJ2hLUW0zTh597g448JbTfg2DToux7WjcXG8W1WdX0PN3pgyTi6iLsmou8NTH2D4U8DN0cM1mOe52IhhFetmpxD8AIoI8UZzvTwfkROCUEAnKw7ZyXmkSR2aVTNh08Y62+CnjCFDDOMBZCDKRwOB7Rti2EYCkOWZst4Ib/V7881Hm7u8LA7ovUZUWhzMcZYAFhfznPOheGyLMWncY+yZpxWgQFZkJBsSpL2CriIP8nXGFMOKDgPKRuuMQPG6fNbLQVqHZOI0/66R9Mc0Y0R1gcRxMHPoQCR0XNm9LUMKlqlVEvNBov91+/Y7VrsbUZrfBFleZaSpsdBQPVPopxvkFuXtNZF2ig8pck5b6dIBFFGTJNiKVvaGWBKvPHvbewrKsyDBCO4Aocn8XrpY/qK8wIwnaXGjTygPci4slG1/1X5AiBTiqULVqW36vbfGr4EZK00Ja5xTnBVe8suArJtosT591Bx7Tn6vEjb5dQ2W4CqDP/t7DrpTwKsrwkPKcvNx2ELMOd1flvf4urde3y5ruWxCCfA1+wRb4Xi+u5K/AgAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"api policy setuptest\"\n        title=\"api policy setuptest\"\n        src=\"/static/6b801a2d606784a2defd6ab9f2f02bfb/fcda8/api-policy-setuptest.png\"\n        srcset=\"/static/6b801a2d606784a2defd6ab9f2f02bfb/12f09/api-policy-setuptest.png 148w,\n/static/6b801a2d606784a2defd6ab9f2f02bfb/e4a3f/api-policy-setuptest.png 295w,\n/static/6b801a2d606784a2defd6ab9f2f02bfb/fcda8/api-policy-setuptest.png 590w,\n/static/6b801a2d606784a2defd6ab9f2f02bfb/efc66/api-policy-setuptest.png 885w,\n/static/6b801a2d606784a2defd6ab9f2f02bfb/c83ae/api-policy-setuptest.png 1180w,\n/static/6b801a2d606784a2defd6ab9f2f02bfb/e72de/api-policy-setuptest.png 1198w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></a></p>\n<ul>\n<li>\n<p>Click Send and then in the Http Response click on Trace to see how the request was processed.</p>\n<p>Aside from the headers and requests parms as you would expect you will also see the rate-limit-by-key processing result</p>\n</li>\n</ul>\n<p><a href=\"../images/api-policy-trace-result.PNG\"><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 35.13513513513513%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAx0lEQVQoz12QSQ7FMAhDe/9DdtOmUzrPO74ekqP0L1yoCQZTjONowzAYUeD/PE97nsfu+3Yov64rxRziChoRqOvaxfq+t7ZtPXZdZ03TpDr8uq6ObdsS9n13Di0XrKrKyrK0EIJHmmOMLooYOWIM4D93RG2eZ+eO47CCj2yxsqzmdmX5n8v7kmVWXpblY4OJCOs+5AwGOZ/fEt4tT9P0uRU5gNcwhLQhzbojb6hzQw1yyxS5hyKPeMymRBre93XQBA+4nwZL8AeVbxVcv68pJwAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"api policy trace result\"\n        title=\"api policy trace result\"\n        src=\"/static/97866ebdcbe28b0039c341e25b8cf42f/fcda8/api-policy-trace-result.png\"\n        srcset=\"/static/97866ebdcbe28b0039c341e25b8cf42f/12f09/api-policy-trace-result.png 148w,\n/static/97866ebdcbe28b0039c341e25b8cf42f/e4a3f/api-policy-trace-result.png 295w,\n/static/97866ebdcbe28b0039c341e25b8cf42f/fcda8/api-policy-trace-result.png 590w,\n/static/97866ebdcbe28b0039c341e25b8cf42f/e628c/api-policy-trace-result.png 619w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></a></p>\n<ul>\n<li>\n<p>The one thing that is not ideal is that I cannot see how to test this as one of the test users as you cannot override the subscription being used for the test request. In this case I added my own user to the Customer1 group to see how it is processed.</p>\n<p>It does work as I can send requests from user 1 (who is in the Customer1 group) until the API returns a 429. At this point requests from user 2 (who is also in the Customer1 group) elicits a 429 but user 3 (who is in the Customer2 group) gets a 200.</p>\n<p>Lovely.</p>\n</li>\n</ul>\n<p>It's got some <a href=\"https://azure.microsoft.com/en-gb/blog/policy-expressions-in-azure-api-management/\" target=\"_blank\" rel=\"noopener noreferrer\">documentation</a> but not too much covering this particularly niche application.</p>\n<p>More? See also <a href=\"https://docs.microsoft.com/en-us/azure/api-management/policy-samples\" target=\"_blank\" rel=\"noopener noreferrer\">policy samples</a> , <a href=\"https://docs.microsoft.com/en-us/azure/api-management/api-management-policies\" target=\"_blank\" rel=\"noopener noreferrer\">policies</a> , <a href=\"https://github.com/uglide/azure-content/blob/master/articles/api-management/api-management-sample-flexible-throttling.md\" target=\"_blank\" rel=\"noopener noreferrer\">throttling</a> and <a href=\"https://social.msdn.microsoft.com/Forums/azure/en-US/dc13cf81-f92b-4f05-bf0b-d724e4f4ec01/consulting-ratelimitbykey-policy-of-azure-api-management?forum=azureapimgmt\" target=\"_blank\" rel=\"noopener noreferrer\">a more complex expression example</a>.</p>","frontmatter":{"date":"October 08, 2018","slug":"/blog/azure-apim-custom-policy-for-rate-limiting","title":"Apply custom policy-based rate limiting to Azure APIs using Azure API Management"}}},"pageContext":{"slug":"/blog/azure-apim-custom-policy-for-rate-limiting"}},"staticQueryHashes":["3649515864"]}